package cn.buaa.anniversary.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Created this one by HMH on 2017/5/12.
 */
public class FileHelper {
    private static Logger logger = LoggerFactory.getLogger(FileHelper.class);

    public static File createFile(String path, MultipartFile multipartFile) throws IOException {
        //去除空格并生成文件
        File file = new File(path.replaceAll("%20", " "));
        createDic(file);
        //存储文件
        multipartFile.transferTo(file);
        logger.info(" ===> 文件存储至" + file.getAbsolutePath());
        return file;
    }

    public static boolean createDic(File file) {
        //查看文件夹目录情况
        File dic = new File(file.getParent());
        if (!dic.exists()) {
            //不存在则递归新建
            dic.mkdirs();
        }
        return true;
    }
}
